package com.rongshu.view.discovery.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.ControlScrollLayoutManager;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.BaseFragmentListener;
import com.rongshu.view.discovery.adapter.RecommendHistoryAdapter;
import com.rongshu.view.discovery.contract.SearchView;
import com.rongshu.view.discovery.presenter.SearchPresenter;
import com.rongshu.view.home.bean.LabelResponse;
import com.rongshu.widget.LineBreakLayout;
import com.widget.titlebar.ImmersionBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/1.
 * description：推荐-搜索页面
 */
public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchView {
    @BindView(R.id.search_content_et)
    RelativeLayout search_content_et;
    @BindView(R.id.search_right_tv)
    TextView searchRightTv;
    @BindView(R.id.search_left_tv)
    EditText searchLeftTv;
    @BindView(R.id.lineBreakLayout)
    LineBreakLayout searchTags;
    @BindView(R.id.search_history)
    RecyclerView searchHistory;
    @BindView(R.id.history_head)
    RelativeLayout historyHead;

    private RecommendHistoryAdapter historyAdapter;
    private List<String> historyList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar bar = ImmersionBar.with(this);
        bar.reset().keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  //软键盘自动弹出
        if (ImmersionBar.isSupportStatusBarDarkFont()) {//是否支持修改状态栏字体颜色
            bar.statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
                    .flymeOSStatusBarFontColor(R.color.color_2c2c2c)  //修改flyme OS状态栏字体颜色
                    .init();  //必须调用方可沉浸式
        } else {
            bar.flymeOSStatusBarFontColor(R.color.black)  //修改flyme OS状态栏字体颜色
                    //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                    .statusBarDarkFont(true, 1.0f)
                    .init();  //必须调用方可沉浸式
        }
        bar.init();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        searchLeftTv.setHint("搜索用户、文章");
        //历史搜索
        ControlScrollLayoutManager linearLayoutManager = new ControlScrollLayoutManager(this);
        linearLayoutManager.setScrollEnabled(false);
        searchHistory.setLayoutManager(linearLayoutManager);
        historyAdapter = new RecommendHistoryAdapter(this);
        historyAdapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                String msg = (String) object;
                if (TextUtils.equals(msg, "clearData")) {
                    historyHead.setVisibility(View.GONE);
                } else {
                    LabelResponse.ResponseBean bean = new LabelResponse.ResponseBean();
                    bean.setTitle(msg);
                    presenter.toSearchResult(bean);
                }
            }
        });
        searchHistory.setAdapter(historyAdapter);
        searchTags.setItemListener(new BaseFragmentListener() {
            @Override
            public void onCallBack(Object object) {
                if (object instanceof LabelResponse.ResponseBean) {
                    LabelResponse.ResponseBean bean = (LabelResponse.ResponseBean) object;
                    presenter.toSearchResult(bean);
                }
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new SearchPresenter(this), this);
        presenter.getLabel();
        historyList = presenter.getHistory();
        if (historyList.size() != 0) {
            historyAdapter.setData(presenter.sortList(historyList));
        } else {
            historyHead.setVisibility(View.GONE);
        }
        searchLeftTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                historyList = presenter.getHistory();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String msg = v.getText().toString();
                    if (!historyList.contains(msg)) {//更新历史记录
                        historyList.add(msg);
                        historyAdapter.setData(presenter.sortList(historyList));
                        presenter.saveHistory(historyList);//存入sp
                    }
                    if (historyHead.getVisibility() == View.GONE) {
                        historyHead.setVisibility(View.VISIBLE);
                    }
                    LabelResponse.ResponseBean bean = new LabelResponse.ResponseBean();
                    bean.setTitle(msg);
                    presenter.toSearchResult(bean);
                }
                return false;
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @OnClick({R.id.search_right_tv, R.id.search_left_tv, R.id.clear_history})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_right_tv:
                showKeyboard(false);
                finish();
                break;
            case R.id.clear_history:
                presenter.clearHistory();
                historyList.clear();
                historyAdapter.setData(historyList);
                historyAdapter.notifyDataSetChanged();
                historyHead.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void callBackTags(List<LabelResponse.ResponseBean> response) {
        searchTags.setLables(response, true);
    }
}
